package com.myjob.handlerImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.common.DubboConfig;
import com.myjob.config.MjServiceName;
import com.myjob.entity.Company;
import com.myjob.entity.Hr;
import com.myjob.handler.HrHandler;
import com.myjob.mapper.CompanyMapper;
import com.myjob.mapper.HrMapper;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Chen Yuan
 * @create 2021-01-28  15:03
 */
@Slf4j
@Component
@Service(group = MjServiceName.COMPANY,
		retries = DubboConfig.RETRIES,
		timeout = DubboConfig.TIME_OUT,
		version = DubboConfig.VERSION)
public class HrHandlerImpl implements HrHandler {
	@Autowired
	private HrMapper hrMapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Override
	public Hr selectOneHr(String hrId) {
		log.info("[Dubbo] - 被远程调用 -> HrHandler -> selectOneHr("+hrId+")");
		Hr hr = null;
		if (UtilValidate.isNotEmpty(hrId)){
			QueryWrapper<Hr> wrapper = new QueryWrapper<>();
			wrapper.eq("user_id", hrId);
			hr = hrMapper.selectOne(wrapper);
		}
		Company company = null;
		if (UtilValidate.isNotEmpty(hr)) {
			QueryWrapper<Company> wrapper = new QueryWrapper<>();
			wrapper.eq("company_id", hr.getCompanyId());
			company = companyMapper.selectOne(wrapper);
			hr.setCompany(company);
		}
		return hr;
	}

	@Override
	public int updateHr(Hr hr) {
		log.info("[Dubbo] - 被远程调用 -> HrHandler -> updateHr("+hr.getUserId()+")");
		if (UtilValidate.isEmpty(hr)){
			return -1;
		}
		return hrMapper.updateById(hr);
	}

	@Override
	public Hr selectOneHrAndCom(String hrId) {
		log.info("[Dubbo] - 被远程调用 -> HrHandler -> selectOneHr("+hrId+")");
		Hr hr = null;
		if (UtilValidate.isNotEmpty(hrId)){
			QueryWrapper<Hr> wrapper = new QueryWrapper<>();
			wrapper.eq("user_id", hrId);
			hr = hrMapper.selectOne(wrapper);
		}
		return hr;
	}
}
